package com.leantech.android.browser.ui.search.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.leantech.android.browser.databinding.ItemCurrentUrlBinding
import com.leantech.android.browser.utils.onThrottleClick

class CurrentUrlAdapter(private val callback: CurrentUrlCallback) :
    RecyclerView.Adapter<CurrentUrlAdapter.CurrentUrlViewHolder>(
    ) {



    private var title: String = ""
    private var url: String = ""

    fun updateContent(title: String, url: String) {
        this.title = title
        this.url = url
        notifyItemChanged(0)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CurrentUrlViewHolder {
        val binding =
            ItemCurrentUrlBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return CurrentUrlViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return 1
    }

    override fun onBindViewHolder(holder: CurrentUrlViewHolder, position: Int) {
        holder.bind(title, url, callback)
    }


    class CurrentUrlViewHolder(private val binding: ItemCurrentUrlBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(
            title: String,
            url: String,
            callback: CurrentUrlCallback
        ) {
            if(url.isBlank()) return
            binding.tvSearchLink.text = title
            binding.tvSearchLinkAddress.text = url
            binding.btnCopy.onThrottleClick {
                callback.onCopyClicked(url)
            }
        }
    }


    interface CurrentUrlCallback {
        fun onCopyClicked(url: String)

    }
}